package com.achun.getcouponleague.ui.adapter;

import android.content.Intent;
import android.graphics.Paint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.achun.getcouponleague.Bean.CategoriesContent;
import com.achun.getcouponleague.R;
import com.achun.getcouponleague.base.IRecyclerList;
import com.achun.getcouponleague.ui.activity.TiketActivity;
import com.achun.getcouponleague.utils.Logutils;
import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class RecycleviewAdapter extends RecyclerView.Adapter<RecycleviewAdapter.Myholder> {
    private List<IRecyclerList> category_content_list=new ArrayList<>();
    int index=1;
    @NonNull
    @Override
    public Myholder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
       // Logutils.e(this.getClass(),"createView-->"+index);
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.home_recycle_view_item, parent, false);
        index+=1;
        return new Myholder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull Myholder holder, int position) {
        int itemposition=position;
       // Logutils.e(this.getClass(),"bindview-->");
        holder.setDatas(category_content_list.get(position),position);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               if (recycleViewOnClickListener!=null)
               recycleViewOnClickListener.onclick(itemposition,category_content_list.get(itemposition));
            }
        });

    }
    //点击事件接口
    private RecycleViewOnClickListener recycleViewOnClickListener;
    public interface RecycleViewOnClickListener {
        void onclick(int position,IRecyclerList item);
    }
    public void setRecycleViewOnClickListener(RecycleViewOnClickListener recycleViewOnClickListener){
        this.recycleViewOnClickListener=recycleViewOnClickListener;
    }

    @Override
    public int getItemCount() {
        return category_content_list.size();
    }

    public void setDatas(List<? extends IRecyclerList> categorypagelist) {
        category_content_list.clear();
        category_content_list.addAll(categorypagelist);
        Logutils.e(this.getClass(),""+category_content_list.size());
        notifyDataSetChanged();

    }

    public void addDatas(List<? extends  IRecyclerList> categorypagelist) {
        int oldSize=category_content_list.size();
        category_content_list.addAll(categorypagelist);
        notifyItemRangeChanged(oldSize,categorypagelist.size());

    }

    public class Myholder extends RecyclerView.ViewHolder {
        @BindView(R.id.iv_home_cover)
        public ImageView iv_home_cover;
        @BindView(R.id.tv_goods_title)
        public TextView tv_goods_title;
        @BindView(R.id.tv_red_packet)
        public TextView tv_red_packet;
        @BindView(R.id.discounted_price)
        public TextView discounted_price;
        @BindView(R.id.bought_people_number)
        public TextView bount_people_number;
        @BindView(R.id.tv_old_price)
        public TextView oldPrice;
        public Myholder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);

        }

        public void setDatas(IRecyclerList dataBean, int position) {
            String coverUrl = getString(dataBean);//压缩图片
            Logutils.e(this.getClass(),"图片url：--》"+coverUrl);
            Glide.with(itemView.getContext()).load(coverUrl).into(iv_home_cover);
            tv_goods_title.setText(dataBean.getTitle());
            tv_red_packet.setText("省"+dataBean.getCoupon_amount()+"元");
            float final_price = Float.parseFloat(dataBean.getZk_final_price()) - dataBean.getCoupon_amount();
            discounted_price.setText("￥"+String.format("%.2f",final_price));
            oldPrice.setPaintFlags(Paint.STRIKE_THRU_TEXT_FLAG);
            oldPrice.setText("￥"+dataBean.getZk_final_price());
            bount_people_number.setText("");

        }

        @NonNull
        private String getString(IRecyclerList dataBean) {
            ViewGroup.LayoutParams layoutParams = iv_home_cover.getLayoutParams();
            int height=layoutParams.height;
            int width=layoutParams.width;
            int size=height>width?height:width;
            if (dataBean.getPict_url().startsWith("http")){
                return dataBean.getPict_url()+"_"+size+"x"+size+".jpg";
            }
            String coverUrl="https:"+ dataBean.getPict_url()+"_"+size+"x"+size+".jpg";
            return coverUrl;
        }
    }
}
